package com.mingqijia.gassafety.job.service.asst;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.constant.AsstConstants;
import com.mingqijia.gassafety.shared.constant.MbpConst;
import com.mingqijia.gassafety.shared.dto.OrderCancelDTO;
import com.mingqijia.gassafety.shared.utils.HttpUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;

/**
 * 时刻助手服务【工单】
 * @Author: duanhongxiang
 * @Date: 2024/5/21 18:10
 */
@Component
@Slf4j
public class AsstService {
    @Value(value = "${asst.workOrder.doamin.url}")
    String workOrderUrl;

    @Autowired
    HttpUtil httpUtil;

    public Map<String, Object> queryOrderInfo(String orderNo,String spId) {
        Map<String, Object> result = new HashMap<>();
        int code = 1;
        List<Map<String, Object>> lists = new ArrayList<>();
        
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("orderNo", orderNo);
            params.put("page", 1);
            params.put("pageSize", 10);
            String body = httpUtil.getBody(JSONObject.toJSONString(params),
                    workOrderUrl + AsstConstants.ORDER_LIST_URL_V2,
                    spId);
            JSONObject jsonObject = JSONObject.parseObject(body);
            if (null != jsonObject.get("code") && jsonObject.get("code").equals(0)) {
                JSONArray data = jsonObject.getJSONObject("data").getJSONArray("list");
                code = 0;
                for (int i = 0; i < data.size(); i++) {
                    Map<String, Object> temp = new HashMap<>();
                    if( orderNo.equals(data.getJSONObject(i).getString("orderNo")) ) {
                        int orderStatus = data.getJSONObject(i).getIntValue("orderStatus");
                        if (60 == orderStatus || 80 == orderStatus) { //已完成,归档
                            temp.put("state", MbpConst.WORK_STATE_FINISH);
                        } else if (70 == orderStatus) { //已取消
                            temp.put("state", MbpConst.WORK_STATE_END);
                        } else {
                            temp.put("state", 0);
                        }
                        temp.put("finishtime", data.getJSONObject(i).getString("completedTime"));
                        temp.put("workpkid", data.getJSONObject(i).getString("id"));
                        lists.add(temp);
                        break;
                    }
                }
            }
        }catch (Exception e){
            log.error("获取工单状态报错",e);
        }
        result.put("code", code);
        result.put("data", lists);
        return result;
    }

    /**
     * 取消工单
     * @param params
     */
    public void cancelOrderV2(OrderCancelDTO params) {
        try{
            String body = httpUtil.getBody(JSONObject.toJSONString(params),
                    workOrderUrl + AsstConstants.ORDER_CANCEL_URL_V2,
                    params.getSpId());
            JSONObject jsonObject = JSONObject.parseObject(body);
            if (null == jsonObject) {
                throw new BizException("取消工单未拿到返回结果");
            }else if( null==jsonObject.get("code") || !jsonObject.get("code").equals(0) ) {
                String msg = jsonObject.get("msg")!=null?jsonObject.getString("msg"):"取消工单失败";
                throw new BizException(msg);
            }
        }catch (Exception e){
            log.error("取消工单失败", e);
            throw new BizException(e.getMessage());
        }
    }
    
}
